package com.tiza.xiaoyoubao.app;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.tianze.library.base.BaseActivity;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.utils.CloseActivity;

import java.lang.ref.WeakReference;

import butterknife.ButterKnife;

public class SimpleBackActivity extends BaseActivity {
    public final static String BUNDLE_KEY_PAGE = "BUNDLE_KEY_PAGE";
    public final static String BUNDLE_KEY_ARGS = "BUNDLE_KEY_ARGS";
    private static final String TAG = "FLAG_TAG";
    private WeakReference<Fragment> mFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_simple_back;
    }

    @Override
    protected int getTextColor() {
        return getResources().getColor(R.color.white);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ((AppBarLayout) mToolbar.getParent()).setElevation(1.0f);
        }
        setLeftActionResource(R.mipmap.zuofx);
        mToolbar.setBackgroundResource(R.color.steel_blue);
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            ButterKnife.findById(this, R.id.rootView).setFitsSystemWindows(false);
        }
        Intent data = getIntent();
        if (data == null) {
            throw new RuntimeException("you must provide a page info to display");
        }
        int pageValue = data.getIntExtra(BUNDLE_KEY_PAGE, 0);
        SimpleBackPage page = SimpleBackPage.getPageByValue(pageValue);
        if (page == null) {
            throw new IllegalArgumentException("can not find page by value:" + pageValue);
        }

        hideRightTextAction();
        setToolbarTitle(page.getTitle());
        try {
            Fragment fragment = (Fragment) page.getClz().newInstance();
            Bundle args = data.getBundleExtra(BUNDLE_KEY_ARGS);
            if (args != null) {
                fragment.setArguments(args);
            }
            FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
            trans.replace(R.id.container, fragment, TAG);
            trans.commit();

            mFragment = new WeakReference<>(fragment);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("generate fragment error. by value:" + pageValue);
        }
        CloseActivity.activityList.add(this);

    }

    @Override
    protected void onDestroy() {
        CloseActivity.activityList.remove(this);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        if (mFragment != null && mFragment.get() != null && mFragment
                .get() instanceof BaseFragment) {
            BaseFragment bf = (BaseFragment) mFragment.get();
            if (!bf.onBackPressed()) {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mFragment != null && mFragment.get() != null && mFragment
                .get() instanceof BaseFragment) {
            BaseFragment bf = (BaseFragment) mFragment.get();
            bf.onActivityResult(requestCode, resultCode, data);
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
